﻿myApp.controller('EventController', function ($scope, $http, DUMMY_DATA,$location, $rootScope) {
    $scope.$emit('newPageLoaded', {
        'title': 'Events | ShiJie Property',
        'description': "Latest Events In Property. We Work Very Closely With Developers. Over 40 Years Experience. Discover The Latest Events In Property Today!",
        'keywords': 'latest events, latest events property'
    });

    //$scope.$emit('newPageLoaded', { 'title': 'Penang Realty 世界地產 EVENTS', 'description': 'Join our event to obtain first hand info.' });

    $scope.isAndroid = $rootScope.isAndroid;

    pageInit();

    $scope.sortOption = "Sorted by Latest to Oldest";

    var BASE_URL;
    if ($location.host() == "localhost") {
        BASE_URL = $location.protocol() + "://" + "localhost:" + $location.port();
    } else {
        BASE_URL = $location.protocol() + "://" + $location.host();
    }


    $scope.getShareURL = function (p) {
        //  return (BASE_URL + "/property/" + p.propertyid + "/S" + p.Zoning[0]);
        return (BASE_URL + "/info/events/details/" + p.EventId);
    }

    $scope.getShareTitle = function (p) {
        return p.Name;
    }

    $scope.getShareDescription = function (p) {
        return p.Details;
    }

    $scope.getShareImageURL = function (p) {
        return (p.PropertyURL || "http://www.penangrealty.my/images/penang realty,Apartment,house,condominium,terrace house,semi d,detached,bungalow,high end luxury properties,landed properties,gated guarded house.png");
    }




    $scope.paginate = function (value) {
        var begin, end, index;
        begin = ($scope.currentPage - 1) * $scope.numPerPage;
        end = begin + $scope.numPerPage;
        index = $scope.searchData.indexOf(value);
        return (begin <= index && index < end);
    };
    $scope.updateFilterOption = function (option, sortedChanged) {
        $scope.SortOptionChanged = sortedChanged;
        //$scope.$apply();
        console.log("HEY!" + option);
        switch (option) {
            case 0:
                $scope.sortOption = "Sorted by Latest to Oldest";
                $scope.sortKey = '-CreateDate';
                break;
            case 1:
                $scope.sortOption = "Sorted by Oldest to Latest";
                $scope.sortKey = 'CreateDate';
                break;
            case 2:
                $scope.sortOption = "Sorted by Area (A to Z)";
                $scope.sortKey = 'Title';
                break;
            case 3:
                $scope.sortOption = "Sorted by Area (Z to A)";
                $scope.sortKey = '-Title';
                break;
        }
    }

    $scope.isEmpty = function (obj) {
        for (var i in obj) if (obj.hasOwnProperty(i)) return false;
        return true;
    };

    $scope.is10Selected = true;
    $scope.is25Selected = false;
    $scope.is50Selected = false;
    $scope.is100Selected = false;
    $scope.changePerPage = function (npp) {
        $scope.numPerPage = npp;
        switch (npp) {
            case 10:
                $scope.is10Selected = true;
                $scope.is25Selected = false;
                $scope.is50Selected = false;
                $scope.is100Selected = false;
                break;
            case 25:
                $scope.is10Selected = false;
                $scope.is25Selected = true;
                $scope.is50Selected = false;
                $scope.is100Selected = false;
                break;
            case 50:
                $scope.is10Selected = false;
                $scope.is25Selected = false;
                $scope.is50Selected = true;
                $scope.is100Selected = false;
                break;
            case 100:
                $scope.is10Selected = false;
                $scope.is25Selected = false;
                $scope.is50Selected = false;
                $scope.is100Selected = true;
                break;
        }
        $scope.totalItems = $scope.searchData.length;
        $scope.currentPage = 1;
        $scope.numPerPage = npp;
    }




    function pageInit() {
        if (DUMMY_DATA == true) {
            var dummyData = [{ "EventId": "4b5d470c-aad5-4db4-b1af-db8f661c30fe", "Name": "REAL ESTATE CAREER TALK", "Venue": "11A, Jalan Todak 3, Bandar Sunway, 13700 Seberang Jaya, Pulau Pinang.", "StartDateTime": "2016-04-16T00:00:00", "EndDateTime": "2016-04-16T00:00:00", "Details": "We help you to create a successful Real Estate career, join us for the free Career Talk.\n\nDate : 16/4/2016\nTime : 11am - 1pm\nVenue : 11A, Jalan Todak 3, Pusat Bandar Sunway, Seberang Jaya, 13700 Penang.\n\nRegister at :\nhttp://goo.gl/forms/xBRFGmjBBN", "CreateDate": "2016-03-26T12:20:58.13", "UpdateDate": "2016-03-26T16:46:38.893", "PropertyURL": "https://farm2.staticflickr.com/1635/25436427623_e046149381_b.jpg" }];
            $scope.searchData = dummyData;
            console.log($scope.searchData.length);
            $scope.totalItems = $scope.searchData.length;
            $scope.currentPage = 1;
            $scope.numPerPage = 10;
        } else {
            $http.get('api/Events/GetEvents').success(function (data) {
                console.log("Event: " + JSON.stringify(data))
                $scope.searchData = data;
                //$scope.EventDetails = data.Events.Details.replace(new RegExp('\r?\n', 'g'), '<br />');

                $scope.totalItems = $scope.searchData.length;
                $scope.currentPage = 1;
                $scope.numPerPage = 10;
            }).error(function(err){
                console.log("Error: " + JSON.stringify(err));
            });
        }
      

        
    }


    $scope.submitForm = function (obj) {
        var url = 'https://script.google.com/macros/s/AKfycbwcomu5o2CIbkO1lCjyZhDrF_dejSPWi0VAj9knYKYRr3jHDUY/exec';
        $http({
            method: 'POST',
            url: url,
            data: $(obj.target).serialize(),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function (data) {
            alert("Thanks for Subscribing to our Newsletter!");
            $scope.subscription_email = "";
        });
    }



});